# AUTHOR: DING
# -*- codeing = utf-8 -*-
# @Time: 2025/6/12 14:00
# @Author: 86139
# @Site: 
# @File: VisaManager.py
# @Software: PyCharm
import pyvisa


class VisaManager:
    def __init__(self):
        self.rm = pyvisa.ResourceManager()
        self.resources_dict = {}

    """
    资源的创建与关闭
    """

    def add_resource(self, address: str) -> None:
        resource = self.rm.open_resource(address)
        self.resources_dict[address] = resource

    def get_resource(self, address: str) -> pyvisa.Resource:
        return self.resources_dict[address]

    def close_resource(self, address: str) -> None:
        self.resources_dict[address].close()
        del self.resources_dict[address]

    """
    指令的收发
    """

    def write(self, address: str, command: str) -> None:
        resource = self.get_resource(address)
        resource.write(command)

    def query(self, address: str, command: str) -> str:
        resource = self.get_resource(address)
        return resource.query(command)


vm = VisaManager()
